/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

/**
 *
 * @author nhvkhanh
 */
public class Controller extends HttpServlet {

    private boolean found = false;
    private String fullName="";
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException, ParserConfigurationException, SAXException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           String action = request.getParameter("action");
           DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
           DocumentBuilder db = dbf.newDocumentBuilder();
           ServletContext context = getServletContext();
           String realContextPath = context.getRealPath("/");
           String realPath = realContextPath + "WEB-INF//students.xml";
           File file = new File(realPath);
           Document doc = db.parse(file);
           if (action.equals("Login"))
           {
               String userID = request.getParameter("userID");
               String password = request.getParameter("password");
               found = false;
               checkLogin(userID, password, doc);
               String url = "invalid.jsp";
               if (found)
               {
                   url = "home.jsp";
               }
               RequestDispatcher rd = request.getRequestDispatcher(url);
               rd.forward(request, response);
           }
        } finally { 
            out.close();
        }
    }
    private void checkLogin(String userID, String password, Node node)
    {
        if (node == null || found)
        {
            return;
        }

        if (node.getNodeName().equals("student"))
        {
            NamedNodeMap nodeMap = node.getAttributes();
            for (int i = 0; i<nodeMap.getLength();i++)
            {
                if (nodeMap.item(i).getNodeValue().equals(userID))
                {
                    NodeList listChildNodes = node.getChildNodes();
                    for (int j=0; j<listChildNodes.getLength();j++)
                    {
                        Node child = listChildNodes.item(j);
                        if (child.getNodeName().equals("fullName"))
                        {
                            fullName = child.getTextContent();
                        }
                        if (child.getNodeName().equals("password"))
                        {
                            if (child.getTextContent().equals(password))
                            {
                                found = true;

                            }
                        }
                    }
                }
            }
        }
        NodeList children = node.getChildNodes();
        int i = 0;
        while (i<children.getLength())
        {
            checkLogin(userID, password, children.item(i++));
        }

    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ParserConfigurationException ex) {
            ex.printStackTrace();
        } catch (SAXException ex) {
            ex.printStackTrace();
        }
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
